// components/bar/bar.js
let movabW = 0
let movabv = 0
const bgm = wx.getBackgroundAudioManager()
let currentsec=-1
let duration=0 //歌曲总时长 miao
let ismocing=false//解决冲突
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    showtime: {
      gongingtime: '00:00',
      totaltime: '00:00'
    },
    mDs: 0,
    prs: 0
  },
  lifetimes: {
    ready() {
      this._getMovableDis();
      this._bindbgm();
      console.log(this.data.showtime.totaltime)
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onchange(event){
      //拖动的
      // 拖动
      if (event.detail.source == 'touch') {
        this.data.prs = event.detail.x / (movabW - movabv) * 100
        this.data.mDs = event.detail.x
        ismocing=true
       
        // console.log('change', isMoving)
      }
    },
    onend(){
      
      const go = this._dataForamt(Math.floor(bgm.currentTime))
      this.setData({
        prs:this.data.prs,
        mDs:this.data.mDs,
        ['showtime.gongingtime']: go.min+':'+go.sec
      })
      bgm.seek(duration*this.data.prs/100)
      ismocing=false
    },
    _getMovableDis() {
      const q = this.createSelectorQuery()
      q.select(".movable-area").boundingClientRect()
      q.select(".movable-view").boundingClientRect()
      q.exec((rect) => {
        
        movabW = rect[0].width
        movabv = rect[1].width
        
      })
    },
    _bindbgm: function () {
      bgm.onPlay(() => {
        ismocing=false
        this.triggerEvent('musicplay')
      })
      //播放
      bgm.onStop(() => {
        console.log('stop')
      })
      //停止
      bgm.onPause(() => {
        console.log('pasue')
        this.triggerEvent('musicpause')
      })
      //暂停
      bgm.onWaiting(() => {
        
        
      
      })
      //加载
      bgm.onCanplay(() => {
        console.log('canpplay')
        if (typeof (bgm.duration) != 'undefined') {
          this._setTime()
        } else {
          setTimeout(() => {
            this._setTime()
          }, 1000)
        }
      })
      //能播放状态
      bgm.onTimeUpdate(() => {
       if(!ismocing){
         const currtime = bgm.currentTime
         const duration = bgm.duration
         const currtimeFm = this._dataForamt(currtime)
         const sec = currtime.toString().split('.')[0]
         if (sec != currentsec) {
           this.setData({
             mDs: (movabW - movabv) * currtime / duration,
             prs: currtime / duration * 100,
             ['showtime.gongingtime']: `${currtimeFm.min}:${currtimeFm.sec}`,
           })
           currentsec = sec
           //联动歌词
           this.triggerEvent('timeUpdate',{
             currtime
           })
         }
       }
      })
      //进度
      bgm.onEnded(() => {
        this.triggerEvent('musicEnd')
      })
      //完成
      bgm.onError((err) => {
        console.log(err.errMsg)
        console.log(err.errCode)
        wx.showToast({
          title: '错误' + err.errCode,
        })
      })
    },
    //决定获取时间
    _setTime() {
      duration = bgm.duration
      const durationFmt = this._dataForamt(duration)
      this.setData({
        ['showtime.totaltime']: `${durationFmt.min}:${durationFmt.sec}`
      })
    },
    //格式化时间
    _dataForamt(sec) {
      const min = Math.floor(sec / 60)
      sec = Math.floor(sec % 60)
      return {
        'min': this._add0(min),
        'sec': this._add0(sec),
      }
    },
    //补零
    _add0(sec) {
      return sec < 10 ? '0' + sec : sec
    }
  }
})
